/* 
 * Copyright WestYao.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "xcomponent_window_wrapper.h"
#include "av_codec_sample_log.h"
#include "external_window.h"
#include "sample_window_manager.h"

namespace {
constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_APP, LOG_DOMAIN_TEST, "XCOMPONENT_WINDOW_WRAPPER_BASE"};
}

namespace OHOS {
namespace MediaAVCodec {
namespace Sample {
XComponentWindowWrapper::XComponentWindowWrapper(OHNativeWindow *window)
{
    windowType_ = SampleWindowType::XCOMPONENT;
    window_ = std::shared_ptr<OHNativeWindow>(window, OH_NativeWindow_DestroyNativeWindow);
}

void XComponentWindowWrapper::OnSurfaceCreated(uint64_t surfaceId)
{
    NativeWindow *window = nullptr;
    OH_NativeWindow_CreateNativeWindowFromSurfaceId(surfaceId, &window);

    auto windowWrapper = std::static_pointer_cast<WindowWrapper>(std::make_shared<XComponentWindowWrapper>(window));
    auto ret = WindowManager::GetInstance().RegisterWindowWrapper(windowWrapper, true);
    CHECK_AND_RETURN_LOG(ret != -1, "Register window to window manager failed");
}
} // namespace Sample
} // namespace MediaAVCodec
} // namespace OHOS